<?php
class Chat{
	public function submitChat( $gid, $text )
	{
		if( !isset( $_SESSION['user'] ) ) {
			return array('status'	=>-2); // not logged in
		}
		$uid = $_SESSION['user']['uid'];
		$gid = DB::esc($gid);
		$text = DB::esc($text);
				
		$query  = "SELECT cuid FROM chatuser WHERE gid = '$gid' AND uid = '$uid' AND toshow = 'true'";
		$result = DB::query($query);
		if( $result->num_rows ) {
			$cuid = $result->fetch_object()->cuid;
			
			if( DB::query("INSERT INTO chat (cuid, text) VALUES ('$cuid', '$text')") ) {
				return array('cuid'		=>$cuid,'cid'=>DB::getMySQLiObject()->insert_id,'status'	=>1);
			} else {
				return array('status'	=>-1);
			}
		} else {
			return array('status'	=>0); 
			// have not join the room yet
		}
	}

	public static function getChats( $gid, $lastID, $max_num ){
		if( !isset( $_SESSION['user'] ) ) {
			return -2; // not logged in
		}
		
		$uid = $_SESSION['user']['uid'];
		$gid     =  (int)$gid;
		$lastID  =  (int)$lastID;
		$max_num =  (int)$max_num;
		$query   = "SELECT text, chat.ts AS ts, uid, username, avatar, cid  
					FROM chat
					JOIN chatuser
					USING ( cuid ) 
					JOIN user
					USING ( uid ) 
					WHERE cid > '$lastID'
					AND gid =  '$gid'
					AND chatuser.uid NOT IN ( SELECT uid_to FROM blocked_user WHERE uid_from = '$uid' )
					ORDER BY cid ASC 
					LIMIT 0 , $max_num";
		$result  = DB::query( $query );
		$chats   = array();
		Galaxy::visit_galaxy( $gid );
		$mg_flag = get_magic_quotes_gpc();
		while($chat = $result->fetch_object()){
			if( $mg_flag ) {
				$chat->text = stripslashes( $chat->text ); 
			}
			// Returning the GMT (UTC) time of the chat creation:
			$chat->time = array(
				'hours'		=> gmdate('H',strtotime($chat->ts)),
				'minutes'	=> gmdate('i',strtotime($chat->ts))
			);
			$chats[] = $chat;
		}
		return array(
				'lastID'=> $lastID,
				'gid'	=> $gid,
				'chats' => $chats
		);
	}
	
	public static function getLatestChats($gid, $max_num){
		if( !isset( $_SESSION['user'] ) ) {
			return -2; // not logged in
		}
		$uid = $_SESSION['user']['uid'];
		$gid	= (int)$gid;
		$max_num = (int)$max_num;
		$query   = "SELECT * FROM(SELECT text, chat.ts AS ts, uid, username, avatar, cid  
					FROM chat
					JOIN chatuser
					USING ( cuid ) 
					JOIN user
					USING ( uid ) 
					WHERE gid =  '$gid' AND chatuser.uid NOT IN ( SELECT uid_to FROM blocked_user WHERE uid_from = '$uid' )
					ORDER BY cid DESC 
					LIMIT 0 , $max_num)r
					ORDER BY cid ASC";
		$result  = DB::query( $query );
		$chats   = array();
		Galaxy::visit_galaxy( $gid );
		$mg_flag = get_magic_quotes_gpc();
		while($chat = $result->fetch_object()){
			if( $mg_flag ) {
				$chat->text = stripslashes( $chat->text ); 
			}
			// Returning the GMT (UTC) time of the chat creation:
			$chat->time = array(
				'hours'		=> gmdate('H',strtotime($chat->ts)),
				'minutes'	=> gmdate('i',strtotime($chat->ts))
			);
			$chats[] = $chat;
		}
		return array(
				'lastID'=> $lastID,
				'gid'	=> $gid,
				'chats' => $chats
		);
	}
	
	public static function getAllChats() {
		$query = "SELECT icon, chatuser.uid AS uid, chatuser.gid AS gid, text, username, avatar, chat.ts AS ts, title
					FROM chat
					JOIN chatuser
					USING ( cuid ) 
					JOIN user
					USING ( uid ) 
					JOIN galaxy
					USING ( gid )";
		$result = DB::query($query);
		$chats = array();
		$mg_flag = get_magic_quotes_gpc();
		while($chat = $result->fetch_object()){	
			if( $mg_flag ) {
				$chat->text = stripslashes( $chat->text ); 
			}
			$chats[] = $chat;
		}
		return $chats;
	}
	
	public static function getChatsLog($gid){
		if( !isset( $_SESSION['user'] ) ) {
			return -2; // not logged in
		}
		$gid = (int)$gid;
		$uid = $_SESSION['user']['uid'];
		$result = DB::query("SELECT icon, chatuser.uid AS uid, chatuser.gid AS gid, text, username, avatar, chat.ts AS ts, title
					FROM chat
					JOIN chatuser
					USING ( cuid ) 
					JOIN user
					USING ( uid ) 
					JOIN galaxy
					USING ( gid ) WHERE gid='$gid' AND chatuser.uid NOT IN ( SELECT uid_to FROM blocked_user WHERE uid_from = '$uid' )
				    ORDER BY ts ASC");
		$chats = array();
		$mg_flag = get_magic_quotes_gpc();
		while($chat = $result->fetch_object()){
			if( $mg_flag ) {
				$chat->text = stripslashes( $chat->text ); 
			}
			$chats[] = $chat;
		}
	
		return array(
			'chats' => $chats,
			'gid'=> $gid
		);
	}
	
	public static function blockUser( $uid_to ) {
		$uid_to   = (int)$uid_to;
		$uid_from = $_SESSION['user']['uid'];
		
		if( $uid_to == $uid_from ) {
			return -1; // he can not block himself
		}
		
		if( DB::query( "INSERT INTO blocked_user ( uid_from, uid_to ) VALUES ( '$uid_from', '$uid_to' )" ) ) {
			return 1;
		} else {
			return 0;// already blocked
		}
	}
	public static function unblockUser( $uid_to ) {
		$uid_to   = (int)$uid_to;
		$uid_from = $_SESSION['user']['uid'];
		
		if( $uid_to == $uid_from ) {
			return -1; // he can not block himself
		}
		if( DB::query( "DELETE FROM blocked_user WHERE uid_from = '$uid_from' AND uid_to = '$uid_to'" ) ) {
			return 1;
		} else {
			return 0;// already blocked
		}	
	}
}
?>